package com.uenpay.im.ui.old.activity.movement;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.weight.listview.RefreshListView;
import com.uenpay.util.app.base.BaseActivity;

import java.text.MessageFormat;
import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-25 下午4:50:26
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class SignMovementListActivity extends BaseActivity implements
        RefreshListView.IRefreshListViewListener {

    private MovementMembersAdapter adapter = null;
    private RefreshListView xListView;
    private int page = 1;
    private MovementHandler movementHandler = new MovementHandler();
    private View loading;
    private TextView no_data;
    private int movement_id;
    private int count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_xlist_layout;
    }

    @Override
    public void initView(View view) {
        movement_id = getIntent().getIntExtra("movement_id", 0);
        count = getIntent().getIntExtra("count", 0);
        initView();
        initListener();
        loading.setVisibility(View.VISIBLE);
        getData();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview)).setText(MessageFormat
                .format(getResources()
                        .getString(R.string.movement_joined_count), count));
        xListView = (RefreshListView) findViewById(R.id.discover_listview);
        loading = findViewById(R.id.loading);
        no_data = (TextView) findViewById(R.id.no_data_tv);
    }

    protected void initListener() {
        xListView.setPullLoadEnable(true);
        xListView.setRefreshListViewListener(this);

        xListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                QYXUserEntity entity = (QYXUserEntity) arg0.getAdapter()
                        .getItem(arg2);
                if (entity != null) {
                    ActivityHandle.startUserDetailActivity(SignMovementListActivity.this, entity.custid);
                }
            }

        });
    }

    private void getData() {
        movementHandler.getMovementMembersList(page, movement_id,
                new MovementHandler.IMovementMembersListener() {

                    @Override
                    public void onMembersResultListener(int status, String msg,
                                                        ArrayList<QYXUserEntity> arrayList) {
                        onLoad();
                        loading.setVisibility(View.GONE);
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            if (page == 1) {
                                if (arrayList == null || arrayList.size() < 1) {
                                    no_data.setVisibility(View.VISIBLE);
                                    xListView.setVisibility(View.GONE);
                                } else {
                                    if (adapter == null) {
                                        adapter = new MovementMembersAdapter(
                                                SignMovementListActivity.this,
                                                arrayList);
                                    } else {
                                        adapter.setData(arrayList);
                                    }
                                    xListView.setAdapter(adapter);
                                    no_data.setVisibility(View.GONE);
                                    xListView.setVisibility(View.VISIBLE);
                                    xListView.setPullLoadEnable(true);
                                }
                            } else {
                                if (arrayList != null && arrayList.size() > 0) {
                                    adapter.addData(arrayList);
                                } else {
                                    xListView.setPullLoadEnable(false);
                                }
                            }
                        } else {
                            if (page == 1) {
                                if (arrayList == null || arrayList.size() < 1) {
                                    no_data.setVisibility(View.VISIBLE);
                                    xListView.setVisibility(View.GONE);
                                }
                            }
                        }
                    }
                });
    }

    private void onLoad() {
        xListView.stopRefresh();
        xListView.stopLoadMore();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }

}
